package com.drcurd.controller;

import com.drcurd.entity.Product;
import com.drcurd.dto.ProductQueryDTO;
import com.drcurd.service.ProductService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品控制器
 */
@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 分页查询产品列表
     */
    @GetMapping("/page")
    public Map<String, Object> getProductList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = new HashMap<>();
        try {
            PageInfo<Product> pageInfo = productService.getProductList(page, size);
            result.put("success", true);
            result.put("data", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据条件分页查询产品
     */
    @GetMapping("/search")
    public Map<String, Object> searchProducts(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Double minPrice,
            @RequestParam(required = false) Double maxPrice,
            @RequestParam(required = false) Long brandId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        Map<String, Object> result = new HashMap<>();
        try {
            ProductQueryDTO queryDTO = new ProductQueryDTO();
            queryDTO.setName(name);
            queryDTO.setMinPrice(minPrice != null ? BigDecimal.valueOf(minPrice) : null);
            queryDTO.setMaxPrice(maxPrice != null ? BigDecimal.valueOf(maxPrice) : null);
            queryDTO.setBrandId(brandId);
            queryDTO.setPageNum(page);
            queryDTO.setPageSize(size);
            
            PageInfo<Product> pageInfo = productService.getProductListByCondition(queryDTO);
            result.put("success", true);
            result.put("data", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询所有产品（不分页）
     */
    @GetMapping
    public Map<String, Object> getAllProducts() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Product> products = productService.getAllProducts();
            result.put("success", true);
            result.put("data", products);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 根据ID查询产品
     */
    @GetMapping("/{id}")
    public Map<String, Object> getProductById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Product product = productService.getProductById(id);
            if (product != null) {
                result.put("success", true);
                result.put("data", product);
                result.put("message", "查询成功");
            } else {
                result.put("success", false);
                result.put("message", "产品不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 添加产品
     */
    @PostMapping
    public Map<String, Object> addProduct(@RequestBody Product product) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = productService.addProduct(product);
            if (success) {
                result.put("success", true);
                result.put("message", "添加成功");
            } else {
                result.put("success", false);
                result.put("message", "添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 更新产品
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = productService.updateProduct(product);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除产品
     */
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteProduct(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = productService.deleteProduct(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败：" + e.getMessage());
        }
        return result;
    }
} 